/*
 * MobileAgent.java
 *
 * Created on 12 de Fevereiro de 2008, 00:14
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package br.ufal.ic.mobile;

import jade.Boot;
import jade.core.AID;
import jade.core.Agent;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.ServerSocket;
import java.util.Properties;
import java.util.Vector;

import br.ufal.ic.mobile.behaviours.MobileBehaviour;
import br.ufal.ic.mobile.behaviours.Sensor;

/**
 *
 * @author Raphael
 */
public class MobileAgent extends Agent {
    private String nameAgent;
    private String addressAgent;
    private String arg;
    private Vector hosts;
    
    public MobileAgent() {
        
    }
    
    /** Creates a new instance of MobileAgent */
    public MobileAgent(AID a, String arg) {
        ServerSocket socket;
        boolean done = false;
        int port = 1100;
        
        while (!done) {
            try {
                System.out.println("port = "+port);
                socket = new ServerSocket(port);
                System.out.println("passou!");
                socket.close();
                Boot.main(new String [] {"-port",String.valueOf(port),"-services", "jade.core.mobility.AgentMobilityService;" +
                        "jade.core.migration.InterPlatformMobilityService", "mobileAgent:br.ufal.ic.mobile.MobileAgent("+a.getName()+" "+a.getAddressesArray()[0]+" "+arg+")"});            
                done = true;
            } catch (IOException ex) {
                port++;
            }            
        }      
    }

    protected void setup() {
        Object[] args = getArguments();
        
        this.nameAgent = args[0].toString();
        this.addressAgent = args[1].toString();
        
        if (args != null) {
            for (int i=2; i<args.length; i++) {
                if (this.arg == null) this.arg = args[i].toString();
                else this.arg += " "+args[i].toString();
            }
        }
        
        System.out.println("this.nameAgent[MobileAgent] = "+this.nameAgent);
        System.out.println("this.addressAgent[MobileAgent] = "+this.addressAgent);
        System.out.println("this.arg[MobileAgent] = "+this.arg);

        Properties prop = new Properties();
        this.hosts = new Vector();        
        try {
            prop.load(new FileInputStream("agent.properties")); 
            parseItinerary(prop);
        } catch (FileNotFoundException ex) {
            ex.printStackTrace();
        } catch (IOException ex) {
            ex.printStackTrace();
        }                                        
        
        addBehaviour(new MobileBehaviour(this.arg, 0, this.hosts));
    }    

    protected void afterMove() {
        System.out.println("Entrou no afterMove!");
        addBehaviour(new Sensor(this.nameAgent, this.addressAgent));        
    }
    
    private void parseItinerary(Properties p){
        String prop = "";
        int i = 0;
        while(!(prop = p.getProperty("hop" + i, "")).equals("")) {
            hosts.add(prop);
            i++;
        }
    }
}
